package com.alan.ssh.action;

import com.alan.ssh.model.Product;
import com.alan.ssh.service.ProductService;
//import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;


/**
 * 商品操作-控制层
 * @author Alan
 * @Description
 * @date 2020年11月30日 21:48
 */
@Controller
@Scope("prototype")
//@Slf4j
public class ProductAction extends SuperAction{

    private static final long serialVersionUID = 1L;
    @Autowired
    private ProductService productService;
    private String pname;
    private double price;

    /**
     * 保存商品操作
     *
     * @return
     */
    public String saveProduct() {
        Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
        logger.trace("trace level");
        logger.debug("debug level");
        logger.info("info level");
        logger.warn("warn level");

        Product product = new Product(pname, price);
        productService.saveProduct(product);
        //log.info("保存成功...");
        this.addActionMessage("保存成功...");
        return SUCCESS;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;

    }

    @Override
    public void validate() {
        if(pname == null || "".equals(pname.trim())) {
            this.addFieldError("pname", "商品名称不能为空");
        }
    }
}
